<?php

namespace app\model;

class User extends UserBase
{
    public function roles()
    {
        return $this->belongsToMany(Role::class, UserRole::class, 'role_id', 'user_id');
    }

    public static function onBeforeWrite($user)
    {
        if (!empty($user->password)) {
            $user->password = md5($user->password);
        }
        $user->token = md5($user->id);
    }
}
